//
//  CS_Page_ImageView.m
//  Pods
//
//  Created by 网新中研 on 17/3/1.
//
//

#import "CS_Page_ImageView.h"



@interface CS_Page_ImageView()<UIScrollViewDelegate>

@property (nonatomic, strong) UILabel *markLabel;

@property (nonatomic, strong) UILabel *infoLabel;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIPageControl *pageControl;


@end

@implementation CS_Page_ImageView

+(instancetype)page_ImageView:(NSString *)title images:(NSArray *)images mark:(NSString *)mark{
    CS_Page_ImageView *page_ImageView = [CS_Page_ImageView new];
    page_ImageView.title = title;
    page_ImageView.images = images;
    page_ImageView.mark = mark;
    return page_ImageView;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    [super layoutView:superView primaryY:primaryY];
    
    CGFloat localY = 0;
    
    //标题
    self.titleLabel.text = [self isNullOrEmpty:_title] ? @"" : [NSString stringWithFormat:@"%@", _title];
    self.titleLabel.frame = CGRectMake(CELL_RIGHT_MARGIN, CELL_TOP_MARGIN, CELL_TITLE_WIDTH, 0);
    [self.titleLabel sizeToFit];
    CGFloat needTitleHeight = self.titleLabel.frame.size.height;
    needTitleHeight = needTitleHeight > CELL_LABLE_HEIGHT? needTitleHeight : CELL_LABLE_HEIGHT;
    [self addSubview:self.titleLabel];
    
    localY = 2 * CELL_TOP_MARGIN + needTitleHeight;
    
    if (_images == nil
        || _images.count == 0) {//当没有对应图片的时候
        
        self.infoLabel.frame = CGRectMake(CELL_RIGHT_MARGIN + CELL_TITLE_WIDTH - 20, CELL_TOP_MARGIN, kWindowW - 2 * CELL_RIGHT_MARGIN - CELL_TITLE_WIDTH, 0);
        [self.infoLabel sizeToFit];
        CGFloat needDataHeight = self.infoLabel.frame.size.height;
        needDataHeight = needDataHeight > CELL_LABLE_HEIGHT ? needDataHeight : CELL_LABLE_HEIGHT;
        
        [self addSubview:self.infoLabel];
        
        localY = localY > CELL_LABLE_HEIGHT ? localY:CELL_LABLE_HEIGHT ;
        
        self.frame = CGRectMake(0, primaryY, kWindowW, localY);
        
        [superView addSubview:self];
        
        return primaryY + localY;
        
    }else{
        
        if (_images.count == 1) {//当只有一张图片的时候
            
            UIImage *image = [_images objectAtIndex:0];
            CGSize size = image.size;
            CGFloat imageWidth = kWindowW - self.leftMargin - self.rightMargin;
            CGFloat imageHeight = imageWidth * size.height / size.width;
            
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.image = image;
            imageView.frame = CGRectMake(self.leftMargin, localY, imageWidth, imageHeight);
            UIButton *button = [[UIButton alloc]init];
            button.frame = imageView.frame;
            [button addTarget:self action:@selector(showBigImageClick) forControlEvents:UIControlEventTouchUpInside];
            
            [self addSubview:imageView];
            [self addSubview:button];
            
            localY += imageHeight + CELL_TOP_MARGIN;
            self.frame = CGRectMake(0, primaryY, kWindowW, localY);
            [superView addSubview:self];
            
            return primaryY + localY;
            
        }else{
            
            //第一张图片的处理
            UIImage *firstImage = [_images objectAtIndex:0];
            CGSize firstSize = firstImage.size;
            CGFloat firstImageWidth = kWindowW - 2 * CELL_RIGHT_MARGIN;
            CGFloat firstImageHeight = firstImageWidth * firstSize.height / firstSize.width;
            UIImageView *firstImageView = [[UIImageView alloc]init];
            firstImageView.image = firstImage;
            firstImageView.frame = CGRectMake(CELL_RIGHT_MARGIN, 0, firstImageWidth, firstImageHeight);
            
            self.scrollView.frame = CGRectMake(0, localY, kWindowW, firstImageHeight);
            [self.scrollView addSubview:firstImageView];
            [self addSubview:self.scrollView];
            
            UIButton *button = [[UIButton alloc]init];
            button.frame = firstImageView.frame;
            [button addTarget:self action:@selector(showBigImageClick) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
            
            for (int i = 1; i < _images.count; i++) {
                
                UIImage *image = [_images objectAtIndex:i];
                CGSize size = image.size;
                
                UIImageView *imageView = [[UIImageView alloc]init];
                imageView.image = image;
                
                //不同宽高比的处理
                if (size.height/size.width > firstSize.height > firstSize.width) {
                    CGFloat imageHeight = firstImageHeight;
                    CGFloat imageWidth = imageHeight * size.width / size.height;
                    imageView.frame = CGRectMake(i * kWindowW + CELL_RIGHT_MARGIN + (firstImageWidth - imageWidth)/2.0, 0, imageWidth, imageHeight);
                    [self.scrollView addSubview:imageView];
                }else{
                    
                    CGFloat imageWidth = firstImageWidth;
                    CGFloat imageHeight = imageWidth * size.height / size.width;
                    imageView.frame = CGRectMake(i * kWindowW + CELL_RIGHT_MARGIN, (firstImageHeight - imageHeight)/2.0, imageWidth, imageHeight);
                    [self.scrollView addSubview:imageView];
                }
                
                
                UIButton *button = [[UIButton alloc]init];
                button.frame = imageView.frame;
                [button addTarget:self action:@selector(showBigImageClick) forControlEvents:UIControlEventTouchUpInside];
                [self.scrollView addSubview:button];
                
            }
            
            localY += firstImageHeight + CELL_TOP_MARGIN;
            self.scrollView.contentSize = CGSizeMake(_images.count * kWindowW, 0);
            
            self.pageControl.frame = CGRectMake(0, localY - 30, 0, 20);
            
            self.frame = CGRectMake(0, primaryY, kWindowW, localY);
            [superView addSubview:self];
            return primaryY + localY;
            
        }
    }
    
    return primaryY;
}

#pragma mark --- Event
-(void)showBigImageClick{
    
    UIImage *image;
    if (_images.count == 1) {
        image = [_images objectAtIndex:0];
        
    }else{
        image = [_images objectAtIndex:self.pageControl.currentPage];
    }
    
    CS_Page_ImageViewer *page_ImageViewer = [CS_Page_ImageViewer page_imageViewer:image];
    [page_ImageViewer popShow];
    
    
}

#pragma mark --- UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    
    self.pageControl.currentPage = offsetX / kWindowW;
}

#pragma mark --- Lazy loading

-(UILabel *)infoLabel{
    if (_infoLabel == nil) {
        _infoLabel = [[UILabel alloc]init];
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.font = [UIFont systemFontOfSize:(FONT_SIZE_REM)];
        _infoLabel.textColor = COLOR_TEXT_2;
        _infoLabel.text = @"暂无";
    }
    return _infoLabel;
}

-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIPageControl *)pageControl{
    if (_pageControl == nil) {
        _pageControl = [UIPageControl new];
        _pageControl.numberOfPages = _images.count;
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}

-(UILabel *)markLabel{
    if (_markLabel == nil) {
        _markLabel = [[UILabel alloc]init];
        _markLabel.textColor = COLOR_TEXT_2;
    }
    return _markLabel;
}

@end
